// 实现这个项目的构建任务
const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')
const serveStatic = require('serve-static')
const lrSnippet = require('connect-livereload')({
    port: 35729
})
module.exports = grunt => {
    grunt.initConfig({
        clean: {
            folder: ['dist','temp','.tmp']
        },
        web_swig: {
            options: {
                swigOptions:{
                    cache: false
                },
                getData: function(tpl){
                    return {
                        menus: [],
                        pkg: require('./package.json'),
                        date: new Date()
                    };
                }
            },
            your_target: {
                files: {
                    'temp/index.html': 'src/index.html',
                    'temp/about.html': 'src/about.html'
                }
            },
        },
        sass: {
            options: {
                implementation: sass
            },
            main: {
                files: {
                    'temp/assets/styles/main.css': 'src/assets/styles/main.scss'
                }
            }
        },
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            main: {
                files: {
                    'temp/assets/scripts/main.js': 'src/assets/scripts/main.js'
                }
            }
        },
        imagemin: {
            dynamic: {
                files: [
                    {expand: true, cwd: 'dist', src: ['**/*.{png,jpg,gif}'], dest: 'dist'},
                ]
            }
        },
        copy: {
            main: {
              files: [
                {expand: true, cwd: 'src', src: ['assets/fonts/**','assets/images/**'], dest: 'dist'},
                {expand: true, cwd: 'public', src: ['**'], dest: 'dist'},
                {expand: true, cwd: 'temp', src: ['*.html'], dest: 'dist'},
              ],
            },
        },
        connect: {
            options: {
                port: 8080,
                open: {
                    target: 'http://localhost:8080/index.html',
                }
            },
            livereload: {
                options: {
                    // 通过LiveReload脚本，让页面重新加载。
                    middleware: function(connect, options) {
                        return [
                            // 把脚本，注入到静态文件中
                            lrSnippet,
                            // 静态文件服务器的路径
                            //serveStatic(options.base[0]),
                            serveStatic('temp'),
                            serveStatic('src'),
                            serveStatic('public'),
                            serveStatic('.'),
                        ]
                    },
                }
            }
        },
        watch: {
            page: {
                files: ['src/*.html'],
                tasks: ['web_swig'],
                options: {
                    livereload: 35729,
                },
            },
            js: {
                files: ['src/assets/scripts/*.js'],
                tasks: ['babel'],
                options: {
                    livereload: 35729,
                },
            },
            css: {
                files: ['src/assets/styles/*.scss'],
                tasks: ['sass'],
                options: {
                    livereload: 35729,
                },
            }
        },
        useminPrepare: {
            html: 'temp/*.html',
            options: {
              dest: 'dist',
              root: ['temp', '.']
            }
        },
        usemin: {
            html: 'dist/*.html',
        },
 
    })
    loadGruntTasks(grunt)
    /* grunt.loadNpmTasks('grunt-contrib-clean')
    grunt.loadNpmTasks('grunt-web-swig')
    grunt.loadNpmTasks('grunt-sass')
    grunt.loadNpmTasks('grunt-babel')
    grunt.loadNpmTasks('grunt-contrib-watch')
    grunt.loadNpmTasks('grunt-contrib-connect')
    grunt.loadNpmTasks('grunt-contrib-concat')
    grunt.loadNpmTasks('grunt-contrib-copy')
    grunt.loadNpmTasks('grunt-contrib-uglify')
    grunt.loadNpmTasks('grunt-usemin') */
    grunt.loadNpmTasks('grunt-contrib-cssmin')
    const dev = ['sass','babel','web_swig']
    grunt.registerTask('dev', dev.concat(['connect','watch']))
    grunt.registerTask('build', ['clean'].concat(dev, ['copy', 'useminPrepare', 'concat', 'uglify', 'cssmin', 'usemin', 'imagemin']))
}